<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="主体名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'ztname', validatorRules.ztname]" placeholder="请输入主体名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="法人代表" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'frname', validatorRules.frname]" placeholder="请输入法人代表姓名"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="经营地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'address', validatorRules.address]" placeholder="请输入经营地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="企业类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag type="list_multi" v-decorator="['companyType', validatorRules.companyType]"
                                  :trigger-change="true" dictCode="company_type" placeholder="请选择企业类型"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="员工人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'memberNumber', validatorRules.memberNumber]" placeholder="请输入员工人数"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="带贫人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'helpNumber', validatorRules.helpNumber]" placeholder="请输入带贫人数"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="批次" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  第7批次格式：7
                </template>
                <a-input v-decorator="[ 'batch', validatorRules.batch]" placeholder="请输入批次"></a-input>
              </a-tooltip>
            </a-form-item>
          </a-col>
          <a-col :lg="12">

          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="主营" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['mainBusiness', validatorRules.mainBusiness]" rows="4" placeholder="请输入主营"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="借款用途" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['borrowingPurposes',, validatorRules.borrowingPurposes]" rows="4"
                          placeholder="请输入借款用途"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JMultiSelectTag from "@/components/dict/JMultiSelectTag"

    export default {
        name: "LoanRecommendationWithPoorEnterprisesModal",
        components: {
            JMultiSelectTag,
        },
        data() {
            return {
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                validatorRules: {
                    ztname: {rules: [{ required: true, message: '主体名称不能为空'}]},
                    frname: {rules: [{ required: true, message: '法人代表不能为空'}]},
                    address: {rules: [{ required: true, message: '经营地址不能为空'}]},
                    companyType: {rules: [{ required: true, message: '企业类型不能为空'}]},
                    memberNumber: {},
                    helpNumber: {},
                    batch: {rules: [{ required: true, message: '批次不能为空'}]},
                    mainBusiness: {},
                    borrowingPurposes: {},
                },
                url: {
                    add: "/loanmanagement/loanRecommendationWithPoorEnterprises/add",
                    edit: "/loanmanagement/loanRecommendationWithPoorEnterprises/edit",
                }

            }
        },
        created() {
        },
        methods: {
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'ztname', 'frname', 'address', 'companyType', 'memberNumber', 'helpNumber', 'batch', 'mainBusiness', 'borrowingPurposes'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'ztname', 'frname', 'address', 'companyType', 'memberNumber', 'helpNumber', 'batch', 'mainBusiness', 'borrowingPurposes'))
            }

        }
    }
</script>